#ifndef __ECU_SECU_H__
#define __ECU_SECU_H__

// platform independ definitions

#if defined(STM32F10X_HD) || defined(STM32F10X_MD) || defined(STM32F10X_SD)
#  define _STM32F10X
#endif

#ifdef _STM32F10X
#  include "common.h"
#  ifndef __NOW__
#    define __NOW__ ((int32_t) OSTimeGet())
#  endif // __NOW__
#elif defined(_WIN32)
#endif // _STM32F10X

#include "../htod.h"
#include "../../stm32/common/ds18b20.h"
// #include "../../stm32/common/enc28j60.h"
#include "../../stm32/common/net.h"
#include "../../stm32/common/ip_arp_udp_tcp.h"

#define MAX_NIC_SEND_FAIL   (5)	  // the continuous NIC send fail before trigger NIC reset

#define PORT_WWW              80
#define GROUP_PORT_BASE       5204
#define GROUP_PORT_Adm        (GROUP_PORT_BASE)
#define GROUP_PORT_CanOverIP  (GROUP_PORT_BASE+1)
#define THIS_IP               {192,168,0,NODEID_TO_IP_B4(HT_NODEID)}
#define GROUP_IP              {239,0,0,55}

// NodeId is 7bit, its higher 3bit defines the ID of ECU, and the lower
// 4bit defines the extension nodes connected to this ECU thru interface fdext
// b7             0 
// +-+-+-+-+-+-+-+
// | ecu |  ext  |
// +-+-+-+-+-+-+-+
#define SUBNET_SEGMENT_MASK (0x70)
#define SUBNET_SEGMENT_BITS (4)
#define NODEID_TO_IP_B4(_NODEID)           (0xa0 | (_NODEID>>SUBNET_SEGMENT_BITS)) // start since decimal 160

enum _TxtMsg_ChId
{
	// yield 0~5 for nRF channels
	MSG_CH_CAN1            =6,
	MSG_CH_RS232_Received  =8, 
	MSG_CH_RS232_SendTo,
	MSG_CH_RS485_Received,
	MSG_CH_RS485_SendTo,
	BASE_IP_SOCKET         =0x20, // the socketId
};

extern uint8_t fdadm;
extern uint8_t fdext;
extern uint8_t fdcan;

extern const uint8_t  MyIP[4];  // = THIS_IP;
extern const uint16_t MyServerPort; // PORT_WWW
extern const uint8_t  GroupIP[4]; // = GRUP_IP

// about global state word flags
#define _gState         Ecu_globalState

#define DEBUG_CONST  (dsf_Debug & _deviceState)

// about local state flags
#define _deviceState    Ecu_localState
#define _gState         Ecu_globalState

#define MotionMask_Outer    0x03

void State_init(bool debugMode);
void State_process(void);
uint16_t State_scanDevice(uint16_t hintedSleep);
void State_enter(StateOrdinalCode newState);

// APIs about message gateway
void GW_dispatchTextMessage(uint8_t fdin, char* msg);
void AdminExt_sendMsg(uint8_t fdout, char* msg, uint8_t len);

// =========================================================================
// External IO Resources
// =========================================================================
#define CHANNEL_SZ_DS18B20     (8)
#define CHANNEL_SZ_MOTION     (10)
#define CHANNEL_SZ_IrLED       (4)
#define CHANNEL_SZ_IrRecv      (4)
#define CHANNEL_SZ_Relay       (4)
#define CHANNEL_SZ_LUMIN       (8)

#ifndef CHANNEL_SZ_ADC
#  define CHANNEL_SZ_ADC          CHANNEL_SZ_LUMIN	+1
#  define CHANNEL_SZ_Temperature  (8)
#endif // CHANNEL_SZ_ADC

extern DS18B20 ds18b20s[CHANNEL_SZ_DS18B20];
extern uint8_t packetEth[MTU_SIZE+1];

uint16_t MotionState(void);
void ECU_setRelay(uint8_t id, uint8_t on);

// per profile IrSend()s
void IrSend_PT2262(uint8_t chId, uint32_t code);
void IrSend_uPD6121G(uint8_t chId, uint16_t customCode, uint8_t data);

void ThreadStep_do1msecTimerProc(void);
int  ThreadStep_doStateScan(int nextSleep);
void ThreadStep_doRecvTTY(uint8_t chId, char* buf, uint8_t byteRead, uint8_t* pStartOffset);
int  ThreadStep_doMsgProc(void);
void ThreadStep_doCanMsgIO(void);

#define trace_tty(...) MsgLine_log(MSG_CH_RS232_SendTo, __VA_ARGS__)

#ifdef _STM32F10X
#  define trace trace_tty
#else
#  define trace(...) printLine(__VA_ARGS__)
#endif //  _STM32F10X

#endif // __ECU_SECU_H__
